package cn.kgc.easybuy.controller;

import cn.kgc.easybuy.annotaion.CheckPermission;
import cn.kgc.easybuy.pojo.Product;
import cn.kgc.easybuy.service.ProductService;
import cn.kgc.easybuy.utils.AjaxResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

@RestController
public class ProductController extends BaseController{

    @Autowired
    private ProductService productService;

    @CheckPermission({"0","1"})
    @GetMapping("/delProduct")
    @ApiOperation(value = "删除商品")
    @ApiImplicitParam(name = "id",value = "删除商品id",required = true)
    public AjaxResult delProduct(Integer id){
        if (id == null){
            throw new RuntimeException("删除商品id为空");
        }
        boolean flag = productService.delProduct(id);
        if (flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @CheckPermission({"1"})
    @PostMapping("/updateProduct")
    @ApiOperation(value = "修改商品信息")
    @ApiImplicitParam(name = "product",value = "修改商品对象",required = true)
    public AjaxResult updateProduct(@RequestPart("product") Product product,@RequestPart(value = "file",required = false) MultipartFile file){
        if (product == null){
            throw new RuntimeException("修改商品为空");
        }
        if(file != null){
            AjaxResult ajaxResult = uploadFile(AjaxResult.success(),"fileMsg", file);
            if (ajaxResult.get("code").equals(5200)){
                product.setFileName(ajaxResult.get("newFileName").toString());
            }
        }
        boolean flag = productService.updateProduct(product);
        if (flag){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @CheckPermission({"1"})
    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    @ApiOperation(value = "添加商品信息")
    @ApiImplicitParam(name = "product",value = "添加商品对象",required = true)
    public AjaxResult addProduct(@RequestPart("product") Product product,@RequestPart("file") MultipartFile file){
        if (product == null){
            throw new RuntimeException("添加商品为空");
        }
        if(file==null){
            AjaxResult.error().add("fileMsg","文件不能为空");
        }
        AjaxResult ajaxResult = uploadFile(AjaxResult.success(),"fileMsg", file);
        if (ajaxResult.get("code").equals(5200)){
            product.setFileName(ajaxResult.get("newFileName").toString());
            product.setCreateTime(new Date());
            product.setIsDelete(0);
            product.setOrderStock(0);
            product.setSales(0);
            product.setIsCollect(0);
            boolean flag = productService.addProduct(product);
            if (flag){
                return AjaxResult.success();
            }else {
                return AjaxResult.error();
            }
        }
        return AjaxResult.error();
    }

    @CheckPermission({"0","1"})
    @GetMapping("/selectProductById")
    @ApiOperation(value = "查询商品Id")
    @ApiImplicitParam(name = "id",value = "商品id",required = true)
    public AjaxResult selectProductById(Integer id){
        if (id == null){
            throw new RuntimeException("查询商品id为空");
        }
        Product product = productService.selectProductById(id);
        if (product != null){
            return AjaxResult.success().add("product",product);
        }
        return AjaxResult.error();
    }

    @CheckPermission({"0","1"})
    @PostMapping("/selectProductByPage")
    @ApiOperation(value = "分页查询商品")
    @ApiImplicitParam(name = "params",value = "分页查询参数：pageNum,pageSize,name,createTime",required = true)
    public AjaxResult selectProductByPage(@RequestBody Map<String,Object> params){
        return AjaxResult.success().add("pageInfo",productService.selectProductByPageWithEs(params));
    }

}
